/*
 * @Author: ZhaoDong
 * @Date: 2022-07-06 22:50:11
 * @LastEditors: ZhaoDong
 * @LastEditTime: 2022-07-07 00:06:13
 * @Description: vite config
 */
import type { UserConfig, ConfigEnv } from 'vite';
import { loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';

function pathResolve(dir: string) {
    return resolve(process.cwd(), '.', dir);
}

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
    const isBuild = command === 'build';
    console.log('#isBuild:', isBuild);
    const root = process.cwd();

    const env = loadEnv(mode, root);
    console.log('##env', env);
    return {
        resolve: {
            alias: [
                {
                    find: /\/@\//,
                    replacement: pathResolve('src') + '/'
                },
                {
                    find: /\/#\//,
                    replacement: pathResolve('types') + '/'
                }
            ]
        },
        plugins: [vue()],
        server: {
            port: 8989,
            hmr: {
                host: 'localhost',
                port: 8989
            },
            proxy: {
                '/api': {
                    target: '192.168.1.102',
                    changeOrigin: true,
                    rewrite: (path: string) => path.replace(/^\/api/, '')
                }
            }
        }
    };
};
